//
//  NSString+resultCharact.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/1/16.
//  Copyright © 2016年 game. All rights reserved.
//

#import "NSString+HtmlCharact.h"

@implementation NSString (HtmlCharact)

-(NSString *)filterresult{
    
    NSUInteger myLength = [self length];
    NSUInteger ampIndex = [self rangeOfString:@"&" options:NSLiteralSearch].location;
    
    if (ampIndex == NSNotFound) {
        return self;
    }
    
    NSMutableString *result = [NSMutableString stringWithCapacity:(myLength * 1.25)];
    
    
    NSScanner *scanner = [NSScanner scannerWithString:self];
    
    [scanner setCharactersToBeSkipped:nil];
    
    NSCharacterSet *boundaryCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@" \t\n\r;"];
    
    do {
        // Scan up to the next entity or the end of the string.
        NSString *nonEntityString;
        if ([scanner scanUpToString:@"&" intoString:&nonEntityString]) {
            [result appendString:nonEntityString];
        }
        if ([scanner isAtEnd]) {
            return result;
        }
        
        
//        if ([scanner scanString:@"冬天" intoString:NULL])
//            [result appendString:@"夏天"];
//        
//
//        if ([scanner scanString:@"&lt;" intoString:NULL])
//            [result appendString:@"<"];
//        
//        
//        if ([scanner scanString:@"&lt;" intoString:NULL])
//            [result appendString:@"<"];
//        
//        if ([scanner scanString:@"&lt;" intoString:NULL])
//            [result appendString:@"<"];
        
        // Scan either a HTML or numeric character entity reference.
        if ([scanner scanString:@"&amp;" intoString:NULL])
            [result appendString:@"&"];
        
        else if ([scanner scanString:@"&mdash;&mdash;" intoString:NULL])
            [result appendString:@"————"];
        
        else if ([scanner scanString:@"&mdash;" intoString:NULL])
            [result appendString:@"—"];

        
        else if ([scanner scanString:@"&ldquo;" intoString:NULL])
            [result appendString:@"“"];
        
        else if ([scanner scanString:@"&rdquo;" intoString:NULL])
            [result appendString:@"”"];
        
        else if ([scanner scanString:@"&apos;" intoString:NULL])
            [result appendString:@"'"];
        
        else if ([scanner scanString:@"&quot;" intoString:NULL])
            [result appendString:@"\""];
        
        else if ([scanner scanString:@"&lt;" intoString:NULL])
            [result appendString:@"<"];
        
        else if ([scanner scanString:@"&nbsp;" intoString:NULL])
            [result appendString:@" "];
        
        else if ([scanner scanString:@"&gt;" intoString:NULL])
            [result appendString:@">~&middot;"];
        
        else if ([scanner scanString:@"&middot;" intoString:NULL])
              [result appendString:@"~"];
        
        
        else if ([scanner scanString:@"&rsaquo;" intoString:NULL])
            [result appendString:@"{}"];
        
        
        else if ([scanner scanString:@"&#" intoString:NULL]) {
            BOOL gotNumber;
            unsigned charCode;
            NSString *xForHex = @"";
            
            // Is it hex or decimal?
            if ([scanner scanString:@"x" intoString:&xForHex]) {
                gotNumber = [scanner scanHexInt:&charCode];
            }
            else {
                gotNumber = [scanner scanInt:(int*)&charCode];
            }
            
            if (gotNumber) {
                [result appendFormat:@"%C", (unichar)charCode];
                
                [scanner scanString:@";" intoString:NULL];
            }
            else {
                NSString *unknownEntity = @"";
                
                [scanner scanUpToCharactersFromSet:boundaryCharacterSet intoString:&unknownEntity];
                [result appendFormat:@"&#%@%@", xForHex, unknownEntity];
                //[scanner scanUpToString:@";" intoString:&unknownEntity];
                //[result appendFormat:@"&#%@%@;", xForHex, unknownEntity];
                NSLog(@"Expected numeric character entity but got &#%@%@;", xForHex, unknownEntity);
            }
            
        }
        else {
            NSString *amp;
            
            [scanner scanString:@"&" intoString:&amp];      //an isolated & symbol
            [result appendString:amp];
            
        }
        
    }
    while (![scanner isAtEnd]);
    
    return result;
}


@end
